package com.szxx.suit.single;

import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.szxx.constant.DdzCardTypes;
import com.szxx.domain.Card;
import com.szxx.domain.Cards;
import com.szxx.domain.DdzCard;
import com.szxx.suit.AbstractSuitTerm;
import com.szxx.suit.AbstractSuitType;
import com.szxx.suit.DdzAnalysisResult;
import com.szxx.suit.DdzSuitAnalyzer;
import com.szxx.suit.DdzTypeUtil;

/**
 * 单张牌型 <br>
 *
 * @author Ambrose
 */
@Component
public class SingleTypeAnalyzer extends DdzSuitAnalyzer {

	@Override
	public AbstractSuitType getSuitType() {
		return DdzCardTypes.SINGLE;
	}
	
	/** 关键数量 */
	public static int TUPLE_SIZE = 1;
	
	@Override
	protected boolean doCheckRule(Cards cards, AbstractSuitTerm term, DdzAnalysisResult result) {
		return result.getCardNum() == TUPLE_SIZE;
	}
	
	@Override
	protected List<Cards> doAnalysizeLaizi(Cards cards, AbstractSuitTerm term, boolean onlyMaxValue) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		for (Card card : cards) {
			((DdzCard) card).changeCard(card.getNumber());
		}
		Cards cardsCopy = cards.clone();
		analysize(cardsCopy, term);
		cardsResult.add(cardsCopy);
		return cardsResult;
	}

	@Override
	protected boolean containsPossibleCards(Cards handCards, AbstractSuitTerm term) {
		return ((DdzAnalysisResult) handCards.getResult()).getCardNum() >= TUPLE_SIZE;
	}
	
	/** 最多使用癞子的数量 */
	private static int MAX_USE_LAIZI_CARD_NUM = 1;
	
	@Override
	protected List<Cards> doListPossibleCards(Cards handCards, AbstractSuitTerm term) {
		List<Cards> cardsResult = new LinkedList<Cards>();
		// 非癞子部分
		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
		List<Card> noneLaiziCardList = handCards.subList(0, result.getNoneCardNum());
		Cards noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
		noneLaiziCards.setResult(handCards.getResult().clone());
		// 癞子部分
		List<Card> laiziCardList = handCards.subList(result.getNoneCardNum(), result.getCardNum());
		Cards laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
		laiziCards.setResult(handCards.getResult().clone());
		
		int laiziCardNum = result.getLaziCardNum();
		int maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum ? MAX_USE_LAIZI_CARD_NUM : laiziCardNum;
		for (int useLaiziCardNum = 0; useLaiziCardNum <= maxUseLaiziCardNum; useLaiziCardNum++) {
			int needCardNum = TUPLE_SIZE - useLaiziCardNum;
			if (needCardNum > 0) { // 部分用癞子的情况
				List<Card> useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 0, useLaiziCardNum);// 补癞子
				List<Cards> cardsList = DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum);
				for (Cards usecards : cardsList) {
					Card card = usecards.get(0);
					for (Card laiziCard : useLaiziCardList) {
						((DdzCard)laiziCard).changeCard(card.getValue());
					}
					Cards cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
					if (match(cardsCopy, term)) {
						analysize(cardsCopy, term);
						cardsResult.add(cardsCopy);
					}
				}
			} else { // 全部用癞子的情况
				List<Cards> useLaiziCardList = DdzTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
				for (Cards useLaiziCards : useLaiziCardList) {
					for (Card card : useLaiziCards) {
						((DdzCard)card).changeCard(card.getNumber());
					}
					Cards cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(useLaiziCards, null);
					if (match(cardsCopy, term)) {
						analysize(cardsCopy, term);
						cardsResult.add(cardsCopy);
					}
				}
			}
		}
		return cardsResult;
	}
	
//	@Override
//	protected List<Cards> doListPossibleCards(Cards handCards, AbstractSuitTerm term, boolean onlyMaxValue) {
//		List<Cards> cardsResult = new LinkedList<Cards>();
//		DdzAnalysisResult result = (DdzAnalysisResult) handCards.getResult();
//		List<Card> noneLaiziCardList = handCards.subList(0, result.getNoneCardNum());
//		Cards noneLaiziCards = DdzTypeUtil.createCloneCards(noneLaiziCardList);
//		noneLaiziCards.setResult(handCards.getResult().clone());
//		// 非癞子部分寻找单张
//		cardsResult.addAll(DdzTypeUtil.selectSingleCards(noneLaiziCards, true));
//		if (result.getLaziCardNum() >= 1) { // 考虑癞子
//			List<Card> laiziCardList = handCards.subList(result.getNoneCardNum(), result.getCardNum());
//			Cards laiziCards = DdzTypeUtil.createCloneCards(laiziCardList);
//			// 癞子部分寻找单张
//			laiziCards.setResult(handCards.getResult().clone());
//			List<Cards> singleLaiziCardList = DdzTypeUtil.selectSingleCards(laiziCards, false);
//			for (Cards cards : singleLaiziCardList) {
//				for (Card card : cards) {
//					((DdzCard)card).changeCard(card.getNumber());
//				}
//			}
//			cardsResult.addAll(singleLaiziCardList);
//		}
//		return cardsResult;
//	}

}
